package com.ywb.controller;


import com.ywb.anno.Log;
import com.ywb.pojo.*;
import com.ywb.service.CategoryService;
import com.ywb.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/categories")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/all")
    public Result list(){
        log.info("查询全部类别数据");
        List<Category> categorys = categoryService.findAll();
        return Result.success(categorys);
    }
@GetMapping
public Result page(CategoryQueryParam categoryQueryParam) {
    log.info("分页查询，参数:", categoryQueryParam);
    PageResult<Category> pageResult = categoryService.page(categoryQueryParam);
    return Result.success(pageResult);
}

    @Log
    @DeleteMapping
    public Result delete(Integer id){
//        System.out.println("删除部门"+id);
        log.info("删除类别{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }
    /*新增部门*/
    @Log
    @PostMapping
    public Result add(@RequestBody Category category){

        log.info("新增类别{}",category);
        categoryService.add(category);
        return Result.success();
    }

    //    根据id查询数据 通过pathvariable指定形参绑定 但如果形参名id与传递的请求参数名一致 则可以省略pathvariable
    @GetMapping("/{id}")
    public Result getinfo(@PathVariable("id")Integer categoryid){
//        System.out.println("根据id查询数据"+categoryid);
        log.info("根据id查询数据{}",categoryid);
        Category category = categoryService.getById(categoryid);
        return Result.success(category);
    }
    @PutMapping
    @Log
    public Result update(@RequestBody Category category)
    {

        log.info("修改部门{}",category);
        categoryService.update(category);
        return Result.success();
    }
}
